<?php

namespace app\admin\controller;

use think\Db;

class User extends AdminController
{
    public function _empty()
    {
        return '您访问的操作不存在!!';
    }


    public function index()
    {
        // 原生查询
        $sql = 'SELECT * FROM user';
        $list = Db::query($sql);

        $sql = 'SELECT * FROM user WHERE id = ?';
        $list = Db::query($sql, [3]);

        // DB查询
        $list = Db::table('hc_user')
            ->order(['id' => 'desc'])
            ->select();

        // 助手函数查询
        $list = db('lover')
            ->field(['id', 'name'])
            ->order(['id' => 'asc'])
            ->select();

        dump($list);
    }

    public function insert()
    {
        // 原生新增
//        $sql = 'INSERT INTO user (id, name, age) VALUES (NULL, "k1", 66)';
//        $result = Db::execute($sql);

//        $sql = 'INSERT INTO user (name, age) VALUES (:n, :a)';
//        $result = Db::execute($sql, ['n'=>"k2", 'a'=>'77']);


        // 准备数据
        $data = [
            'name' => 'k5',
            'age' => 44,
        ];
        // DB新增
//        $result = Db::table('user')->insert($data);
//        $result = Db::table('user')->insertGetId($data);

        // 助手函数新增
        $result = db()->table('user')->insertGetId($data);

        dump($result);
    }

    public function delete()
    {
        // 原生删除
//        $sql = 'DELETE FROM user WHERE name = "k1"';
//        $result = Db::execute($sql);

        // DB删除
//        $result = Db::table("user")->delete(55);
//        $result = Db::table("user")->where('name', '大CC')->delete();

        // 助手函数删除
        $result = db()->table("user")->delete(56);
        dump($result);
    }

    public function update()
    {
        // 原生更新
//        $sql = 'UPDATE user SET age = "99" WHERE id = "2"';
//        $result = Db::execute($sql);

        // DB更新
//        $result = Db::table("user")->where('id',1)->update(['age'=>19]);

        // 助手函数更新
        $result = db()->table("user")->where('id',1)->update(['age'=>18]);

        dump($result);
    }


}
